package verify

import (
	"server/game/model"
	"server/share/config"

	"github.com/hwcer/cosgo"
	"github.com/hwcer/logger"
	"github.com/hwcer/updater"
	"github.com/hwcer/yyds/players/verify"
)

func init() {
	cosgo.On(cosgo.EventTypLoaded, func() error {
		verify.SetMethod(config.Data.Functions.PassChapter, PassChapter)
		return nil
	})
}

// PassChapter 通关zh
func PassChapter(u *updater.Updater, value verify.Value) (r int64) {
	coll := u.Handle(config.ITypeChapter)
	if coll == nil {
		return 0
	}
	args := value.GetArgs()
	if len(args) == 0 {
		return 0
	}
	id := args[0]
	cp := config.Data.MainChapter[id]
	if cp == nil {
		logger.Alert("PassChapter config not exist,args:%d", id)
		return 0
	}

	coll.Select(id)
	if err := coll.Data(); err != nil {
		logger.Error(err)
		return 0
	}
	i := coll.Get(id)
	if i == nil {
		return 0
	}
	data := i.(*model.Chapter)
	last := data.Stage[cp.Last]
	if last == nil || last.Pass == 0 {
		return 0
	}

	return 1
}
